package designpatterns.flyweight;

/**
 * 享元模式（Flyweight）
 * 运用共享技术来有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类的开销，从而提高系统资源的复用率。
 * 相同对象只要保存一份，这降低了系统中对象的数量，从而降低了系统中细粒度对象给内存带来的压力。
 * 共享细粒度对象以减少资源占用。
 */
public class Client {
    public static void main(String[] args) {
        FlyweightFactory factory = new FlyweightFactory();
        Flyweight a = factory.getFlyweight("a");
        Flyweight a1 = factory.getFlyweight("a");
        Flyweight a2 = factory.getFlyweight("a");
        Flyweight b = factory.getFlyweight("b");
        Flyweight b1 = factory.getFlyweight("b");

        a.operate(new UnFlyweight("第1次调用a"));
        a1.operate(new UnFlyweight("第2次调用a"));
        a2.operate(new UnFlyweight("第3次调用a"));
        b.operate(new UnFlyweight("第1次调用b"));
        b1.operate(new UnFlyweight("第2次调用b"));
    }
}
